layui.config({
    base: path + '/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'form', 'upload', 'laydate'], function () {
    var $ = layui.$
        , form = layui.form
        , upload = layui.upload
        , laydate = layui.laydate;
    $("#userName").focus();

    var date = new Date();
    var maxdate = date.getFullYear() + '-' + (parseInt(date.getMonth()) + 1) + '-' + date.getDate();
    /*日期控件*/
    var ins = laydate.render({
        elem: "#birthday"
        , trigger: 'click'
        , type: 'date'
        , min: '1900-1-1'
        , max: maxdate
        , btns: ['clear', 'confirm']
        , ready: function () {
            //ins.hint('日期可选值设定在 <br> 1900-1-1 到 '+maxdate+'之间');
        }
    });

    /**
     * 表单验证
     */
    /*密码*/
    $("#passWord").on("blur", function () {
        $("#rePassWord").val("");
        $("#rePassWord").parent().parent().find(".layui-word-aux").text("");
        var msgText = $(this).parent().parent().find(".layui-word-aux");
        if ($(this).val() == "") {
            msgText.text("密码不能为空");
            $(this).focus();
            return;
        }
        if ($(this).val().length < 6) {
            msgText.text("密码长度必须大于等于6位");
            $(this).focus();
            return;
        }
        msgText.text("");
        $(msgText).append('<i class="layui-icon layui-icon-ok" style="color: lawngreen;font-weight: bold"></i>');
    });
    /*确认密码*/
    $("#rePassWord").on("blur", function () {
        var msgText = $(this).parent().parent().find(".layui-word-aux");
        if ($(this).val()==""){
            msgText.text("确认密码不能为空");
            $(this).focus();
            return;
        }
        if ($("#passWord").val() != $(this).val()) {
            msgText.text("两次输入的密码不一致");
            $(this).focus();
            return;
        }
        msgText.text("");
        $(msgText).append('<i class="layui-icon layui-icon-ok" style="color: lawngreen;font-weight: bold"></i>');
    });
    $("#rePassWord").on("focus", function () {
        if ($("#passWord").val() == "") {
            $("#passWord").focus();
            return;
        }
    });

    $("#nikeName").on("blur",function () {
        var msgText = $(this).parent().parent().find(".layui-word-aux");
        if ($(this).val() == ""){
            msgText.text("用户昵称不能为空");
            $(this).focus();
            return;
        }
        msgText.text("");
        $(msgText).append('<i class="layui-icon layui-icon-ok" style="color: lawngreen;font-weight: bold"></i>');
    })

    /*用户名重复性验证*/
    $("#userName").on("blur", function () {
        var msgText = $(this).parent().parent().find(".layui-word-aux");
        if ($(this).val() == ""){
            msgText.text("用户名不能为空");
            $(this).focus();
            return;
        }
        $.ajax({
            url: path + "/main/user/checkedUserName.json",
            type: "GET",
            dataType: "JSON",
            data: {"userName": $(this).val()},
            success: function (data) {
                if (data.isExist == "1") {
                    msgText.text("用户名已存在");
                    $("#userName").focus();
                } else {
                    msgText.text("");
                    $(msgText).append('<i class="layui-icon layui-icon-ok" style="color: lawngreen;font-weight: bold"></i>');
                }
            },
            error: function () {
                layer.msg('检查用户名接口异常,请联系开发者！');
            }
        });
    });

    upload.render({
        elem: '#layuiadmin-upload-useradmin'
        , url: path + '/main/user/headImgUpload.json'
        , accept: 'images'
        , method: 'post'
        , size: 500
        , acceptMime: 'image/*'
        , done: function (res) {
            $(this.item).prev("div").children("input").val(res.fileId);
            $("#uploadImg").attr("src", path + res.src);
        }, error: function () {
            layer.msg('文件上传接口异常,请联系开发者！');
        }
    });

})